package com.zzyl.job;

import com.zzyl.entity.Reservation;
import com.zzyl.mapper.ReservationMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 预约过期任务
 */
@Component
@Slf4j
public class ReservationJob {
    /**
     * 预约过期修改
     * 每半个小时执行一次
     */
    @Autowired
    private ReservationMapper reservationMapper;
    @Scheduled(cron = "0 0/30 * * * ?")
    public void updateReservationStatusTimeoutJob() {
        log.info("执行修改预约状态过期任务: {}", LocalDateTime.now());
        LocalDateTime time = LocalDateTime.now().plusMinutes(-30);
        //查询超时预约任务
        List<Reservation> list = reservationMapper.getTimeoutReservation(time);

        list.forEach(reservation -> {
            reservation.setStatus(3);//0待报道 1已完成 2取消 3过期
            reservation.setRemark("预约超时，系统自动取消");
            reservationMapper.update(reservation);
        });
    }
}
